package mutithread.concurrent;

/**
 * 军队线程，模拟双方作战行为
 * Created by steve
 * 2018/10/17 1:52 PM
 */
public class ArmyRunnable implements Runnable {

    // volatile 保证了线程可以正确地读取其他线程写入的值
    // 可见性 JMM , happens-before 原则
    volatile boolean keepRunning = true;

    @Override
    public void run() {

        while (keepRunning) {
            for (int i=0; i<5; i++) {
                System.out.println(Thread.currentThread().getName() + "进攻对方" + i + "次了！");
            }

            // 让出处理器时间，下次谁进攻还不一定呢。
            Thread.yield();
        }

        System.out.println(Thread.currentThread().getName() + "结束了战斗！");

    }
}
